package oboe.controllers.jml.helpers.scrollBar;

import flash.display.DisplayObjectContainer;
import flash.display.GradientType;
import flash.events.Event;

class VBar extends Bar
{
public function new( container:DisplayObjectContainer )
{
	super( container );
}

public override function update( dragging:Bool=false ):Void
{
	this.container.y = (this.container.y + this.container.mask.y) - int(this.container.height * this.offset);
	this.container.mask.y = int(this.container.height * this.offset);
	
	this.graphics.clear();
	
	var vlimit:Float = this.container.mask.height - (this.isSmall?Bar.CSIZE:0);
	this.container.removeChild( this.container.mask );
	var ratio:Float = this.container.height / vlimit;
	this.container.addChild( this.container.mask );
	if( ratio <= 1 )
	{
	if( !this.isHidden )
	{
		this.emit("hideBar");
	}
	this.isHidden = true;
	return;
	}
	if( this.isHidden )
	{
	this.emit("showBar");
	this.isHidden = false;
	}

	this.graphics.lineStyle( 1, 0x5F5F5F, 1 );
	this.graphics.beginFill( this.barFillColor );
	this.graphics.drawRect( 0, 0, Bar.CSIZE-1, vlimit-1 );
	this.graphics.endFill();
	
	this.gradMat.createGradientBox( Bar.CSIZE, vlimit / ratio, 0 );
	this.graphics.lineStyle( 1, 0x5F5F5F, 1 );
	if( dragging )
	{
	this.graphics.beginGradientFill( GradientType.LINEAR, [this.btnDragColor1, this.btnDragColor2], [1, 1], [0, 255], this.gradMat );
	}
	else
	{
	this.graphics.beginGradientFill( GradientType.LINEAR, [this.btnFillColor1, this.btnFillColor2], [1, 1], [0, 255], this.gradMat );
	}
	this.graphics.drawRect( 0, vlimit * this.offset, Bar.CSIZE-1, vlimit/ratio-1 );
	this.graphics.endFill();
	
	this.y = this.container.y + this.container.mask.y;
	this.x = this.container.x + this.container.mask.x + this.container.mask.width - Bar.CSIZE;
}

protected override function onDrag():Void
{
	var vlimit:Float = this.container.mask.height - (this.isSmall?Bar.CSIZE:0);
	this.container.removeChild( this.container.mask );
	var ratio:Float = this.container.height / vlimit;
	this.container.addChild( this.container.mask );
	var btnSize:Float = vlimit/ratio;
	this.offset = (this.mouseY-btnSize/2) / vlimit;
	if( this.offset < 0 ) this.offset = 0;
	if( this.offset > 1-btnSize/vlimit ) this.offset = 1-btnSize/vlimit;
}

}